package calorie.com.calorie.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.GradeContact;
import calorie.com.calorie.response.ApplyLevelResponce;
import calorie.com.calorie.response.GradeResponce;
import calorie.com.calorie.response.User;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 等级
 * Created by ThinkPad on 2018-11-28.
 */

public class GradeActivity extends MvpActivity<GradeContact.GradePresent> implements GradeContact.IGradeView {
    @BindView(R.id.tv_invite_num)
    TextView tvInviteNum;
    @BindView(R.id.tv_apply_level)
    TextView tvApplyLevel;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;

    @Override
    public void initData(Bundle savedInstanceState) {
        btnConfirm.setBackgroundResource(R.drawable.shape_gray_20_rectangle);
        btnConfirm.setClickable(false);
        mPresent.queryGradeMsg(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_grade;
    }

    @Override
    public void getGradeMsgData(Response<GradeResponce> response) {
        tvInviteNum.setText(response.body().getData() + "");
        String msg = response.body().getMsg();

        if (msg.contains("calorie教练")) {
            tvApplyLevel.setText("calorie教练");
        } else if (msg.contains("calorie大师")) {
            tvApplyLevel.setText("calorie大师");
        } else if (msg.contains("calorie王者")) {
            tvApplyLevel.setText("calorie王者");
        }

        int code = response.body().getCode();
        switch (code) {
            case 200:
                //暂无可申请等级
                btnConfirm.setText("立即申请");
                btnConfirm.setBackgroundResource(R.drawable.shape_gray_20_rectangle);
                btnConfirm.setClickable(false);
                break;
            case 204:
                //申请审核中
                btnConfirm.setBackgroundResource(R.drawable.shape_gray_20_rectangle);
                btnConfirm.setText("审核中");
                btnConfirm.setClickable(false);
                break;
            default:
                btnConfirm.setClickable(true);
                btnConfirm.setBackgroundResource(R.drawable.shape_main_20_rectangle);
                btnConfirm.setText("立即申请");
                ToastUtils.showLongToast(context, response.body().getMsg());
                break;
        }
        User user = StaticValue.user;
        if(user!=null){
            if(user.getLevel()==4){
                tvApplyLevel.setText("你已经是最高等级");
                btnConfirm.setBackgroundResource(R.drawable.shape_gray_20_rectangle);
                btnConfirm.setClickable(false);
            }
        }
        if(msg.equals("暂时不可申请")){
            btnConfirm.setBackgroundResource(R.drawable.shape_gray_20_rectangle);
            btnConfirm.setClickable(false);
        }
    }

    @Override
    public void getApplyLevelData(Response<ApplyLevelResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "提交升级申请成功");
            btnConfirm.setBackgroundResource(R.drawable.shape_gray_20_rectangle);
            btnConfirm.setClickable(false);
            btnConfirm.setText("审核中");
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected GradeContact.GradePresent createPresent() {
        return new GradeContact.GradePresent();
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
            mPresent.applyLevel(context, tvApplyLevel.getText().toString());

    }
}
